package de.tsajar.es.expiration;

import com.jme3.app.Application;
import com.jme3.app.state.AppStateManager;
import com.simsilica.es.Entity;
import com.simsilica.es.EntitySet;
import de.tsajar.es.EntitySystemAppState;

/**
 *
 * @author Eike Foede
 */
public class ExpirationSystem extends EntitySystemAppState{
    private EntitySet entities;

    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        entities = ed.getEntities(ExpirationComponent.class);
    }

    @Override
    public void update(float tpf) {
        entities.applyChanges();
        for(Entity e : entities){
            float secondsToLive = e.get(ExpirationComponent.class).getSecondsToLive() - tpf;
            if(secondsToLive < 0){
                ed.removeEntity(e.getId());
            }else{
                ed.setComponent(e.getId(), new ExpirationComponent(secondsToLive));
            }
            
        }
    }
    
}
